#include "myFunctions.h"
#include "bpNet.h"
int main(int argc,char *argv[]){
	bpNet Network1, Network2;

	float **vRand;
	float **targets1, **targets2;
	vector<float> output1, output2;
	float error1, error2;
	mdouble datos1a, datos1b, datos2a, datos2b;
	int numPatterns = 10000;
	vector<int> y(numPatterns);
  
	vRand = (float**)calloc(numPatterns,sizeof(float *));
	targets1 = (float **)calloc(numPatterns,sizeof(float*));
	targets2 = (float **)calloc(numPatterns,sizeof(float*));
	for (int i=0; i<numPatterns ; i++){
		vRand[i] = (float *)calloc(2,sizeof(float));
		targets1[i] = (float *)calloc(1,sizeof(float));
		targets2[i] = (float *)calloc(1,sizeof(float));
	}     

	for (int i=0;i<numPatterns;i++){
		vRand[i][0] = normRandom() * 6 - 3;
		vRand[i][1] = normRandom() * 6 - 3;
	
		if (fabs(vRand[i][0]) >= 1.0 )
			targets1[i][0] = 1.0;
			else
				targets1[i][0] = (fabs(vRand[i][1]) >=1 ) ? 1.0 : -1.0;
		if (sqrt(vRand[i][0]*vRand[i][0] + vRand[i][1]*vRand[i][1]) >= sqrt(2)/2 )
			targets2[i][0] = 1.0;
			else
				targets2[i][0] = -1.0;
	}

	Network1.Load("cuadrado.txt");
	Network2.Load("circulo.txt");
	
	
	output1.resize(numPatterns);
	output2.resize(numPatterns);
	
	for (int i = 0; i < numPatterns; i++){
		output1[i] = *(Network1.getOutput(vRand[i]));  
    output2[i] = *(Network2.getOutput(vRand[i]));  
		error1 += fabs(targets1[i][0]-output1[i]);
		error2 += fabs(targets2[i][0]-output2[i]);
		vector<double> aux;    
		aux.push_back(vRand[i][0]);
		aux.push_back(vRand[i][1]);
		if(output1[i]>0){
			datos1a.push_back(aux);
		}else{
			datos1b.push_back(aux);
		}
		if(output2[i]>0){
			datos2a.push_back(aux);
		}else{
			datos2b.push_back(aux);
		}
  }
	cout<<"Error de Red 1: "<<error1/numPatterns<<endl;
	cout<<"Error de Red 2: "<<error2/numPatterns<<endl;

	saveFile(datos1a,y,"cuadrado_class_1.txt",false);
	saveFile(datos1b,y,"cuadrado_class_2.txt",false);
	saveFile(datos2a,y,"circulo_class_1.txt",false);
	saveFile(datos2b,y,"circulo_class_2.txt",false);

	Network1.printData("cuadrado_class_1.txt", "cuadrado_class_2.txt");
	Network2.printData("circulo_class_1.txt", "circulo_class_2.txt");
	
	return 1;
}
